package com.SeleniumTestsGoogleChrome;

import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleneseTestBase;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.regex.Pattern;
import static org.junit.Assert.*;


public class LoginAndLogoutTestCases extends SeleneseTestBase {

	@Before
	public void setUp() throws Exception {
		File file = new File(
				"C:/Users/Steven/Documents/4th Year/EECE 419/chromedriver.exe");
		System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
		WebDriver driver = new ChromeDriver();
		String baseUrl = "http://localhost:8080/Website/";
		selenium = new WebDriverBackedSelenium(driver, baseUrl);
	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 1: Successful Login
	// Test Case 1: Enter Valid User Information
	@Test
	public void testUseCase1LoginScenario1TestCase1() throws Exception {
		// Go to Website
		selenium.open("");

		// We are going to Test for 10 login
		for (int i = 0; i < 10; i++) {
			// Pre-Setup Expected Result: On page load, check that a login
			// button is
			// present
			assertTrue(selenium
					.isElementPresent("css=div.tile.bg-color-red > div.tile-content.icon > img"));

			// STEP 1: Click the "Customer" button to login as a regular
			// Customer

			selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
			selenium.waitForPageToLoad("30000");

			// EXPECTED RESULTS 1: Check if the Customer Login page will load

			assertEquals("Customer Login", selenium.getTitle());
			// STEP 2: Enter a valid Username and Password that exists in the
			// Database
			String User = "User" + (i+1);
			String password = "pass" + (i+1);
			selenium.type("name=username", User);
			selenium.type("name=password", password);
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");

			// EXPECTED RESULTS 2: Check if the Customer can view the Customer
			// Portal Page
			assertEquals("Customer Portal", selenium.getTitle());
			assertEquals(User, selenium.getText("class=first-name"));
			selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
			selenium.waitForPageToLoad("30000");

			// EXPECTED RESULTS 3: Checi if the Customer has logged out are
			// redirected back to the main page. Their username should disappear
			// from the top right
			assertEquals("PlaneSimple", selenium.getTitle());
			assertEquals("", selenium.getText("class=first-name"));
		}
	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 1: Successful Login
	// Test Case 2: Enter Valid Admin Information
	@Test
	public void testUseCase1LoginScenario1TestCase2() throws Exception {
		// Go to Website
		selenium.open("");
		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-yellow > div.tile-content.icon > img"));

		// STEP 1: Click the "Customer" button to login as a regular Customer
		selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Login page will load
		assertEquals("Flight Manager Login", selenium.getTitle());

		// STEP 2: Enter a valid ID and Password that exists in the Database
		selenium.type("name=id", "1");
		selenium.type("name=password", "pass0");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if the Customer can view the Customer
		// Portal Page
		assertEquals("Manager Portal", selenium.getTitle());
		assertEquals("1", selenium.getText("class=first-name"));
		selenium.click("css=div.tile.bg-color-red");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 3: Checi if the Customer has logged out are
		// redirected back to the main page. Their username should disappear
		// from the top right
		assertEquals("PlaneSimple", selenium.getTitle());
		assertEquals("", selenium.getText("class=first-name"));
	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 2: Unsuccessful Login
	// Test Case 1: Invalid Password
	@Test
	public void testUseCase1LoginScenario2TestCase1() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-red > div.tile-content.icon > img"));

		// STEP 1: Click the "Customer" button to login as a regular Customer
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Login page will load
		assertEquals("Customer Login", selenium.getTitle());

		// STEP 2: Enter an invalid Password that does not exist in
		// the database
		selenium.type("name=username", "User1");
		selenium.type("name=password", "aaa");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 2: Unsuccessful Login
	// Test Case 2: Invalid Username
	@Test
	public void testUseCase1LoginScenario2TestCase2() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-red > div.tile-content.icon > img"));

		// STEP 1: Click the "Customer" button to login as a regular Customer
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Login page will load
		assertEquals("Customer Login", selenium.getTitle());

		// STEP 2: Enter an invalid Username that does not exist in the database
		selenium.type("name=username", "asdfaesr");
		selenium.type("name=password", "pass1");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 2: Unsuccessful Login
	// Test Case 3: Invalid Username and Password
	@Test
	public void testUseCase1LoginScenario2TestCase3() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-red > div.tile-content.icon > img"));

		// STEP 1: Click the "Customer" button to login as a regular Customer
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Login page will load
		assertEquals("Customer Login", selenium.getTitle());

		// STEP 2: Enter an invalid Username and Password that does not exist in
		// the database
		selenium.type("name=username", "asdfaesr");
		selenium.type("name=password", "asdfasdfaer");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 2: Unsuccessful Login
	// Test Case 4: Missing Information
	@Test
	public void testUseCase1LoginScenario2TestCase4() throws Exception {
		// Go to Website
		selenium.open("");

		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-red > div.tile-content.icon > img"));

		// STEP 1: Click the "Customer" button to login as a regular Customer
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Login page will load
		assertEquals("Customer Login", selenium.getTitle());
		// STEP 2: Enter missing name and password
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

		// Try to Login again
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Login", selenium.getTitle());

		// STEP 3: Enter correct user but a missing password
		selenium.type("name=username", "User1");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 3: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

		// Try to Login again
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Login", selenium.getTitle());

		// STEP 4: Enter no username but a correct password
		selenium.type("name=password", "pass1");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 3: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 2: Unsuccessful Login
	// Test Case 5: Enter Invalid ID Admin Information
	@Test
	public void testUseCase1LoginScenario2TestCase5() throws Exception {
		// Go to Website
		selenium.open("");
		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-yellow > div.tile-content.icon > img"));

		// STEP 1: Click the "Customer" button to login as a regular Customer
		selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Login page will load
		assertEquals("Flight Manager Login", selenium.getTitle());

		// STEP 2: Enter an invalid ID and valid Password that exists in the
		// Database
		selenium.type("name=id", "9999s");
		selenium.type("name=password", "pass0");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 2: Unsuccessful Login
	// Test Case 6: Enter Invalid Password Admin Information
	@Test
	public void testUseCase1LoginScenario2TestCase6() throws Exception {
		// Go to Website
		selenium.open("");
		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-yellow > div.tile-content.icon > img"));

		// STEP 1: Click the "Customer" button to login as a regular Customer
		selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Login page will load
		assertEquals("Flight Manager Login", selenium.getTitle());

		// STEP 2: Enter an valid ID and invalid Password that exists in the
		// Database
		selenium.type("name=id", "User1");
		selenium.type("name=password", "GayPassword");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 2: Unsuccessful Login
	// Test Case 7: Missing Admin Information
	@Test
	public void testUseCase1LoginScenario2TestCase7() throws Exception {

		// Go to Website
		selenium.open("");
		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-yellow > div.tile-content.icon > img"));

		// STEP 1: Click the "Admin" button to login as a regular Customer
		selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Admin Login page will load
		assertEquals("Flight Manager Login", selenium.getTitle());

		// STEP 2: Enter a missing ID
		selenium.type("name=id", "");
		selenium.type("name=password", "pass0");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());

		// Login again
		selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		assertEquals("Flight Manager Login", selenium.getTitle());

		// STEP 3: Enter a missing password
		selenium.type("name=id", "1");
		selenium.type("name=password", "");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// EXPECTED RESULTS 3: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
		// Login again
		selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		assertEquals("Flight Manager Login", selenium.getTitle());

		// STEP 4: Enter a missing password and ID
		selenium.type("name=id", "");
		selenium.type("name=password", "");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// EXPECTED RESULTS 4: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Use Case: Login to Account
	// Scenario 2: Unsuccessful Login
	// Test Case 8: Enter Invalid Password and ID Admin Information
	@Test
	public void testUseCase1LoginScenario2TestCase8() throws Exception {

		// Go to Website
		selenium.open("");
		// Pre-Setup Expected Result: On page load, check that a login button is
		// present
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-yellow > div.tile-content.icon > img"));

		// STEP 1: Click the "Customer" button to login as a regular Customer
		selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 1: Check if the Customer Login page will load
		assertEquals("Flight Manager Login", selenium.getTitle());

		// STEP 2: Enter an valid ID and invalid Password that exists in the
		// Database
		selenium.type("name=id", "FAggot");
		selenium.type("name=password", "GayPassword");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");

		// EXPECTED RESULTS 2: Check if there is an Error Message.
		assertTrue(selenium.isTextPresent("Login"));
		assertTrue(selenium.isTextPresent(""));

		// Go back to Main Page
		selenium.click("//a[contains(@href, 'index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}
	
	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}
}
